package com.sun.rabbit.workQueues.auto_ack;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.sun.rabbit.utils.RabbitMqUtils;
import com.sun.rabbit.utils.SleepUtils;

/**消费者01
 * @author shkstart
 * @create 2021-10-14 16:53
 */
public class Work02 {
    private final static String TASk_QUEUE_NAME="ack_queue";

    public static void main(String[] args) throws Exception {
        common();
    }
    private static void common() throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        int prefetchCount=1;
        channel.basicQos(prefetchCount);
        System.out.println("消费者01等待接收消息处理时间较短：");
        DeliverCallback deliverCallback=(consumerTag, delivery)->{
            String message = new String(delivery.getBody());
            SleepUtils.sleep(1);
            System.out.println("接收到的消息:"+message);
            /**
             * 1. 消息标记 tag
             * 2. 是否批量应答未应答消息
             */
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
        };
        CancelCallback cancelCallback=(consumerTag)->{
            System.out.println(consumerTag+" 消费者取消消费接口回调逻辑");
        };
        // 采用手动应答
        boolean autoAck=false;
        channel.basicConsume(TASk_QUEUE_NAME,autoAck,deliverCallback,cancelCallback);
    }
}
